﻿@page "/DataStandard/DataStandardsSelect"
@attribute [TabItemOption(Text = "标准选择")]
<PageTitle>标准选择</PageTitle>
@inherits ComponentBaseDataCenter
@using Blazor.Diagrams.Components
@using Blazor.Diagrams.Core;
@using Blazor.Diagrams.Core.Models;
@using Blazor.Diagrams.Core.Geometry;
@using OnWork.DataCenter.Shared.Pages.DataStandard.Designer;
@using Newtonsoft.Json;

@*<h3>Http配置</h3>
<HttpConfig />*@

<h3>数据库选择</h3>
<DBConfig />
<h3>标准选择</h3>
@*<div style="width:100%; height: 100vh; user-select: none !important;overflow:scroll;">
    <!-- Add this where you want to show the diagram -->
    <CascadingValue Value="diagram">
        <DiagramCanvas></DiagramCanvas>
    </CascadingValue>
</div>*@
<StandardDesigner/>


@code {

    private Diagram diagram { get;  set; }

    private Table<DataStandardConfigVO>? TableRows { get; set; }

    private List<DataStandardConfigVO>? TreeItems { get; set; }

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();
        // 模拟数据从数据库中获得
        TreeItems = new List<DataStandardConfigVO>() { new DataStandardConfigVO() { Name="tabke"} };
        var options = new DiagramOptions
            {
                DeleteKey = "Delete", // What key deletes the selected nodes/links
                DefaultNodeComponent = null, // Default component for nodes
                AllowMultiSelection = true, // Whether to allow multi selection using CTRL
                Links = new DiagramLinkOptions
                {
                    EnableSnapping = true,
                    DefaultRouter = new Blazor.Diagrams.Core.Router(Routers.Orthogonal),
                    DefaultPathGenerator =  new PathGenerator(PathGenerators.Straight),
                    // Options related to links
                },
                Zoom = new DiagramZoomOptions
                {
                    Minimum = 0.5, // Minimum zoom value
                    Inverse = false, // Whether to inverse the direction of the zoom when using the wheel
                                     // Other
                }
            };

        diagram = new Diagram(options);
        //diagram = new Diagram();
        InitTest();
        //var node1 = NewNode(50, 50);
        //var node2 = NewNode(300, 300);
        //var node3 = NewNode(300, 50);
        //diagram.Nodes.Add(new[] { node1, node2, node3 });

        //diagram.Links.Add(new LinkModel(node1.GetPort(PortAlignment.Right), node2.GetPort(PortAlignment.Left))
        //    {
        //        SourceMarker = LinkMarker.Arrow,
        //        TargetMarker = LinkMarker.Arrow
        //    });
        //diagram.Links.Add(new LinkModel(node2.GetPort(PortAlignment.Right), node3.GetPort(PortAlignment.Right))
        //    {
        //        Router = Routers.Orthogonal,
        //        PathGenerator = PathGenerators.Straight,
        //        SourceMarker = LinkMarker.Arrow,
        //        TargetMarker = LinkMarker.Arrow
        //    });
    }


    private void InitTest()
    {
        diagram.RegisterModelComponent<StandardNode, StandardWidget>();

        var node = new NodeModel(new Point(20, 20));
        node.AddPort(PortAlignment.Top);
        node.AddPort(PortAlignment.Right);
        node.AddPort(PortAlignment.Bottom);
        node.AddPort(PortAlignment.Left);

        diagram.Nodes.Add(new[] { node, NewStandardNode(100, 100), NewStandardNode(300, 300) });
    }

    private StandardNode NewStandardNode(double x, double y)
    {
        var node = new StandardNode(new Point(x, y));
        node.Name = "text";
        node.ID = 123123; 
        node.AddPort(PortAlignment.Bottom);
        node.AddPort(PortAlignment.Top);
        node.AddPort(PortAlignment.Left);
        node.AddPort(PortAlignment.Right);
        return node;
    }

    private void Setup()
    {
        var node1 = NewNode(50, 50);
        var node2 = NewNode(300, 300);
        var node3 = NewNode(300, 50);
        diagram.Nodes.Add(new[] { node1, node2, node3 });
        diagram.Links.Add(new LinkModel(node1.GetPort(PortAlignment.Right), node2.GetPort(PortAlignment.Left)));
    }

    private NodeModel NewNode(double x, double y)
    {
        var node = new NodeModel(new Point(x, y));
        node.AddPort(PortAlignment.Bottom);
        node.AddPort(PortAlignment.Top);
        node.AddPort(PortAlignment.Left);
        node.AddPort(PortAlignment.Right);
        return node;
    }


}
